function [D,n] = xprobks(Q)

% function D = PROBKS(Q) calculates for the Kolmogorov
% -Smirnov statistic d the significance level prob.
% See KSTWO. Adapted from Press, Teukolsky, Vetterling
% and Flannery, Numerical Recipes in Fortran p620.
% Checked using in XPROBKS in Numerical Recipes Example
% Book p185

% Version 1.0 RHS 8/11/93

a2=-2.0*Q.^2;
factor=2;
D=0;
termbf=0;
term=1;
for n=1:100
	term=factor*exp(a2*n^2);
	D=D+term;
	if abs(term)<=0.001*termbf | abs(term)<=1e-8*D
		break
	end
	factor=-factor;
	termbf=abs(term);
end
probks=1;	% get here by failing to converge